<?php
/*
*  Class Name : memcacheInit
*  Date: 10-01-26 by david.peng
*  Desc: For init memcache, read config file, 
*        add memcache server more than 1,
*        set & get memcache data. 
*/
class memcacheInit {
    /*
    * Add memcache server. 
    */
    public static function addServer() {    
        //get memcache config
        $memConfig = ConfigReaderFactory::getInstance()->getConfigReader(AC::REPORT_TOOL_MEMCACHE_FILE)->getConfig();

        //new memcache object
        $hot_search_memcache = new Memcache;
        foreach($memConfig->memcached->hosts as $mValue) {
            $memcacheServer = $mValue->host;
            $memcachePort   = (int)$mValue->port;
            if(!$hot_search_memcache->addServer($memcacheServer,$memcachePort)) {
                //writeLog("Memcache Connect failed :".$memcacheServer.":".$memcachePort,__LINE__);
                return false;
            }
        }
        return $hot_search_memcache;    
    } 
    
    /*
    * Set memcache data.
    */
    public static function setMemcache($key,$content){
		$_memcache = self::addServer();
		if($_memcache){
			$_memcache->set($key,$content);
		}
    }
    
    /*
    * Get memcache data.
    */
    public static function getMemcache($key){
		$_memcache = self::addServer();
        if($_memcache){
			$memcacheArray = $_memcache->get($key);
			return $memcacheArray;
		}else
			return false;
    }  
}
?>